package com.ddone.finance.controller.api;


import com.ddone.base.util.IpUtils;
import com.ddone.base.util.JwtUtils;
import com.ddone.common.exception.Assert;
import com.ddone.common.result.Response;
import com.ddone.common.result.ResponseEnum;
import com.ddone.common.util.RegexValidateUtils;
import com.ddone.finance.pojo.entity.UserInfo;
import com.ddone.finance.pojo.vo.*;
import com.ddone.finance.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author ddone
 * @since 2024-04-10
 */
@RestController
@RequestMapping("/core/api/userInfo")
// @CrossOrigin
@Api(tags = "用户信息")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @ApiOperation("查询会员个人中心数据")
    @GetMapping("/index")
    public Response<Object> userInfoIndex(@RequestHeader(value = "token",required = false)
                                            String token){
        UserIndexVO userIndexVO = userInfoService.getUserIndexVO(token);
        return Response.ok(userIndexVO);
    }

    @ApiOperation("会员注册")
    @PostMapping("/register")
    public Response<Object> register(
            @Valid
            @ApiParam("用户注册对象")
            @RequestBody RegisterVO registerVO) {
        // 手机号不能为空, 验证码不能为空 密码6位以上,密码不能为空
        Assert.isTrue(RegexValidateUtils.checkCellphone(registerVO.getMobile()), ResponseEnum.MOBILE_ERROR);
        String code = stringRedisTemplate.opsForValue().get("sms:code:1:" + registerVO.getMobile());

        Assert.equals(code, registerVO.getCode(), ResponseEnum.CODE_ERROR);
        userInfoService.register(registerVO);
        return Response.ok().message("注册成功");
    }


    @ApiOperation("登录模块")
    @PostMapping("login")
    public Response<String> login(@Valid
                                  @RequestBody LoginVo loginVo,
                                  HttpServletRequest request) {
        // String ip = request.getRemoteAddr();
        String ip = IpUtils.getIpAddressAtService(request);
        String token = userInfoService.login(loginVo, ip);
        return Response.ok(token).message("登录成功");
    }


    @ApiOperation("根据Token查询用户信息")
    @GetMapping("/info")
    public Response<Object> info(@RequestHeader(value = "token", required = false)
                                 @ApiParam("请求头中的token信息")
                                         String token) {
        Long userId = JwtUtils.getUserId(token);
        UserInfo userInfo = userInfoService.getById(userId);
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userInfo, userInfoVO);
        return Response.ok(userInfoVO);
    }

    @ApiOperation("校验手机号是否注册")
    @GetMapping("/checkMobile/{mobile}")
    public Response<Object> checkMobile(@PathVariable("mobile") String mobile){
        return Response.ok(userInfoService.checkMobile(mobile));
    }



    @ApiOperation("判断用户是否绑定手机")
    @GetMapping("/hasBindMobile")
    public Response<Object> checkHasBindMobile(@RequestHeader(value = "token",required = false) String token){
        Boolean flag = userInfoService.hasBindMobile(token);
        return Response.ok(flag);
    }

    @ApiOperation("绑定用户手机号")
    @PutMapping("bindMobile")
    public Response<Object> bindMobile(@RequestHeader(value = "token",required = false) String token,
                                       @ApiParam("绑定手机查询对象")@RequestBody BindMobileVo bindMobileVo){
        Assert.isTrue(RegexValidateUtils.checkCellphone(bindMobileVo.getMobile()), ResponseEnum.MOBILE_ERROR);
        String code = stringRedisTemplate.opsForValue().get("sms:code:2:" + bindMobileVo.getMobile());
        Assert.equals(code, bindMobileVo.getCode(), ResponseEnum.CODE_ERROR);
        Long userId = JwtUtils.getUserId(token);
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(bindMobileVo,userInfo);
        userInfo.setId(userId);
        userInfoService.updateById(userInfo);
        return Response.ok().message("绑定成功");
    }
}

